@@ -1 +0,0 @@ |
||
| 1 |
-require 'ar_mysql_column_charset' |
@@ -1,13 +0,0 @@ |
||
| 1 |
-require 'active_support' |
|
| 2 |
- |
|
| 3 |
-ActiveSupport.on_load :active_record do |
|
| 4 |
- class << ActiveRecord::Base |
|
| 5 |
- def establish_connection(spec = nil) |
|
| 6 |
- super.tap { |ret|
|
|
| 7 |
- if /mysql/i === connection.adapter_name |
|
| 8 |
- require 'ar_mysql_column_charset/main' |
|
| 9 |
- end |
|
| 10 |
- } |
|
| 11 |
- end |
|
| 12 |
- end |
|
| 13 |
-end |
@@ -1,118 +0,0 @@ |
||
| 1 |
-raise "Do not directly load this library." unless defined?(ActiveRecord::ConnectionAdapters::AbstractMysqlAdapter) |
|
| 2 |
- |
|
| 3 |
-module ActiveRecord::ConnectionAdapters |
|
| 4 |
- class ColumnDefinition |
|
| 5 |
- module CharsetSupport |
|
| 6 |
- attr_accessor :charset, :collation |
|
| 7 |
- end |
|
| 8 |
- |
|
| 9 |
- prepend CharsetSupport |
|
| 10 |
- end |
|
| 11 |
- |
|
| 12 |
- class TableDefinition |
|
| 13 |
- module CharsetSupport |
|
| 14 |
- def new_column_definition(name, type, options) |
|
| 15 |
- column = super |
|
| 16 |
- column.charset = options[:charset] |
|
| 17 |
- column.collation = options[:collation] |
|
| 18 |
- column |
|
| 19 |
- end |
|
| 20 |
- end |
|
| 21 |
- |
|
| 22 |
- prepend CharsetSupport |
|
| 23 |
- end |
|
| 24 |
- |
|
| 25 |
- class AbstractMysqlAdapter |
|
| 26 |
- module CharsetSupport |
|
| 27 |
- def prepare_column_options(column, types) |
|
| 28 |
- spec = super |
|
| 29 |
- spec[:charset] = column.charset.inspect if column.charset && column.charset != charset |
|
| 30 |
- spec[:collation] = column.collation.inspect if column.collation && column.collation != collation |
|
| 31 |
- spec |
|
| 32 |
- end |
|
| 33 |
- |
|
| 34 |
- def migration_keys |
|
| 35 |
- super + [:charset, :collation] |
|
| 36 |
- end |
|
| 37 |
- |
|
| 38 |
- def utf8mb4_supported? |
|
| 39 |
- if @utf8mb4_supported.nil? |
|
| 40 |
- @utf8mb4_supported = !select("show character set like 'utf8mb4'").empty?
|
|
| 41 |
- else |
|
| 42 |
- @utf8mb4_supported |
|
| 43 |
- end |
|
| 44 |
- end |
|
| 45 |
- |
|
| 46 |
- def charset_collation(charset, collation) |
|
| 47 |
- [charset, collation].map { |name|
|
|
| 48 |
- case name |
|
| 49 |
- when nil |
|
| 50 |
- nil |
|
| 51 |
- when /\A(utf8mb4(_\w*)?)\z/ |
|
| 52 |
- if utf8mb4_supported? |
|
| 53 |
- $1 |
|
| 54 |
- else |
|
| 55 |
- "utf8#{$2}"
|
|
| 56 |
- end |
|
| 57 |
- else |
|
| 58 |
- name.to_s |
|
| 59 |
- end |
|
| 60 |
- } |
|
| 61 |
- end |
|
| 62 |
- |
|
| 63 |
- def create_database(name, options = {})
|
|
| 64 |
- # utf8mb4 is used in column definitions; use utf8 for |
|
| 65 |
- # databases. |
|
| 66 |
- [:charset, :collation].each { |key|
|
|
| 67 |
- case options[key] |
|
| 68 |
- when /\A(utf8mb4(_\w*)?)\z/ |
|
| 69 |
- options = options.merge(key => "utf8#{$2}")
|
|
| 70 |
- end |
|
| 71 |
- } |
|
| 72 |
- super(name, options) |
|
| 73 |
- end |
|
| 74 |
- end |
|
| 75 |
- |
|
| 76 |
- prepend CharsetSupport |
|
| 77 |
- |
|
| 78 |
- class SchemaCreation |
|
| 79 |
- module CharsetSupport |
|
| 80 |
- def column_options(o) |
|
| 81 |
- column_options = super |
|
| 82 |
- column_options[:charset] = o.charset unless o.charset.nil? |
|
| 83 |
- column_options[:collation] = o.collation unless o.collation.nil? |
|
| 84 |
- column_options |
|
| 85 |
- end |
|
| 86 |
- |
|
| 87 |
- def add_column_options!(sql, options) |
|
| 88 |
- charset, collation = @conn.charset_collation(options[:charset], options[:collation]) |
|
| 89 |
- |
|
| 90 |
- if charset |
|
| 91 |
- sql << " CHARACTER SET #{charset}"
|
|
| 92 |
- end |
|
| 93 |
- |
|
| 94 |
- if collation |
|
| 95 |
- sql << " COLLATE #{collation}"
|
|
| 96 |
- end |
|
| 97 |
- |
|
| 98 |
- super |
|
| 99 |
- end |
|
| 100 |
- end |
|
| 101 |
- |
|
| 102 |
- prepend CharsetSupport |
|
| 103 |
- end |
|
| 104 |
- |
|
| 105 |
- class Column |
|
| 106 |
- module CharsetSupport |
|
| 107 |
- attr_reader :charset |
|
| 108 |
- |
|
| 109 |
- def initialize(*args) |
|
| 110 |
- super |
|
| 111 |
- @charset = @collation[/\A[^_]+/] unless @collation.nil? |
|
| 112 |
- end |
|
| 113 |
- end |
|
| 114 |
- |
|
| 115 |
- prepend CharsetSupport |
|
| 116 |
- end |
|
| 117 |
- end |
|
| 118 |
-end |
@@ -1 +0,0 @@ |
||
| 1 |
-require 'ar_mysql_column_charset' |